package com.example.administrator.newxlmedical;

import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import com.example.administrator.baseproject.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import constants.Constants;
import constants.HandlerConstant;
import utils.ActivityUtil;
import utils.FormateUtils;
import utils.MediaPlayerUtils;


public class OpenDoorRemindActivity extends BaseActivity {
    @BindView(R.id.tv_open_door_remind_cabinet)
    TextView tvCabinet;
    private int type;
    private int selectCabinet;
    private MediaPlayer mediaPlayer;
    private int cabinet;
    private boolean checkIsFirst = true;//用于判断是否是第一次验收
    private SharedPreferences sharedPreferences;
    private int sound;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_door_remind);
        ButterKnife.bind(this);
    }
    @Override
    public void initView() {
        super.initView();
        sharedPreferences = ActivityUtil.getSharedPreferences(this);
        sound = sharedPreferences.getInt(Constants.SOUND_KEY, 50);

        sendEmptyMessageDelay(HandlerConstant.OPEN_DOOR_REMIND_TIME_OUT,60000);
        type = getIntent().getIntExtra(Constants.OPEN_DOOR_REMIND, 0);
        if (type != Constants.INVENTORY) {
            cabinet = getIntent().getIntExtra(Constants.OPEN_DOOR_CABINET,0);
            if (type != Constants.CHECK) {
                if (cabinet != 0) {
                    mediaPlayer = MediaPlayer.create(OpenDoorRemindActivity.this, R.raw.open_door);
                    tvCabinet.setText(cabinet + "号柜柜门已开启");
                }
            }else {
                if (cabinet != 0) {
                    mediaPlayer = MediaPlayer.create(OpenDoorRemindActivity.this, R.raw.checking);
                    tvCabinet.setText(cabinet + "号柜柜门正在验收中...");
                }
            }
            if (mediaPlayer != null ) {
                Float div = FormateUtils.div(sound, 100, 1);
                Log.e(TAG, "initView: " + div );
                mediaPlayer.setVolume(div,div);
                mediaPlayer.start();
            }
        }
    }

    @Override
    protected void onHandleMessage(Message msg) {
        super.onHandleMessage(msg);
        switch (msg.what) {
            case HandlerConstant.CLOSE_DOOR:
                Log.e(TAG, "openDoor: CLOSE_DOOR " + type );
                selectCabinet = (int) msg.obj;
                Intent intent = new Intent();
                if (type == Constants.OPEN) {
                    Log.e(TAG, "openDoor: TakeAndReturnActivity" );
                    intent.setClass(OpenDoorRemindActivity.this, TakeAndReturnActivity.class);
                    intent.putExtra(Constants.TAKE_AND_RETURN_ACTIVITY, selectCabinet);
                } else if (type == Constants.CHECK) {
                    intent.setClass(OpenDoorRemindActivity.this, CheckActivity.class);
                    intent.putExtra(Constants.CHECK_ACTIVITY, selectCabinet);
                    intent.putExtra(Constants.CHECK_ACTIVITY_ISFIRST, checkIsFirst);
                } else if (type == Constants.INVENTORY) {
                    intent.setClass(OpenDoorRemindActivity.this, InventoryActivity.class);
                    intent.putExtra(Constants.INVENTORY_ACTIVITY, selectCabinet);
                } else if (type == Constants.CHECK_AGAIN) {
                    checkIsFirst = false;
                    intent.setClass(OpenDoorRemindActivity.this, CheckActivity.class);
                    intent.putExtra(Constants.CHECK_ACTIVITY, selectCabinet);
                    intent.putExtra(Constants.CHECK_ACTIVITY_ISFIRST, checkIsFirst);
                }
                startActivity(intent);
                finish();
                break;
            case  HandlerConstant.OPEN_DOOR_REMIND_TIME_OUT:
                MediaPlayerUtils.playSound(this,R.raw.open_door);
                sendEmptyMessageDelay(HandlerConstant.OPEN_DOOR_REMIND_TIME_OUT,20000);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) {
            mediaPlayer.release();
        }
    }
}
